{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "d24ddfbe",
   "metadata": {},
   "source": [
    "## 质量检测任务\n",
    "\n",
    "*Copyright (c) 2022 Institute for Quantum Computing, Baidu Inc. All Rights Reserved.*\n",
    "\n",
    "在建筑领域中物理环境、材料等因素会对工程质量产生影响。例如混凝土表面出现裂缝会严重损害建筑物使用寿命。因此工程质量检测至关重要，其中图片分类是一种常用技术。具体来说，我们的任务是给定具体建筑材料图片，然后对其进行二分类，以此判断材料表面是否出现了裂缝。我们使用的数据来自公开数据集 \\[1\\]，其中数据形式如下\n",
    "\n",
    "<img src=\"./surface_crack_example.png\" width=\"20%\" height=\"20%\" div align=center />\n",
    "\n",
    "我们具体使用的训练集包含 1000 张如上图所示的图片，为了测试模型的检测能力，我们选择了 200 张图片作为测试集。"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "15abdd24",
   "metadata": {},
   "source": [
    "## 使用 QNNQD 模型进行裂缝图片检测"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "977c0662",
   "metadata": {},
   "source": [
    "### QNNQD 模型简介\n",
    "\n",
    "QNNQD 模型是一个可以用于图片分类的量子机器学习模型（Quantum Machine Learning，QML）。我们具体称其为一种量子神经网络 (Quantum Neural Network, QNN)，它结合了参数化量子电路（Parameterized Quantum Circuit, PQC）和经典神经网络。对于表面裂缝图片数据，QNNQD 可以达到 90% 以上的分类准确率。模型主要分为量子和经典两部分，结构图如下：\n",
    "\n",
    "<img src=\"./qnnqd_model_cn.png\" width=\"60%\" height=\"60%\"/>\n",
    "\n",
    "模型处理数据的主要过程：\n",
    "1. 通常我们使用主成分分析将图片数据进行降维处理，使得编码电路更容易将经典数据编码为量子态。\n",
    "2. 参数化电路的作用是特征提取，其电路参数可以在训练中被调整，可类比与经典神经网络中的可训练参数。\n",
    "3. 量子测量由一组测量算子表示，是将量子态转化为经典数据的过程，我们可以对得到的经典数据做进一步处理。"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "17661d06",
   "metadata": {},
   "source": [
    "## 模型快速使用"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "f8a3ebf3",
   "metadata": {},
   "source": [
    "### 使用模型进行预测\n",
    "\n",
    "我们可以通过在命令行输入 `python qnn_quality_detection.py --config train.toml` 即可开始训练模型； 这里，我们已经给出了一个训练好的模型，保存格式为 `qnnqd.pdparams`，可以直接用于区分表面裂缝数据。只需要在 `test.toml` 这个配置文件中进行对应的配置，然后在终端输入命令 `python qnn_quality_detection.py --config test.toml` 即可对图片进行预测。为了避免配置过多参数，我们提供了 `example.toml` 文件，可以简单的配置之后执行 `python qnn_quality_detection.py --config example.toml` 完成图片预测。"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "4f739274",
   "metadata": {},
   "source": [
    "### 在线演示\n",
    "\n",
    "下面展示如果通过配置测试文件 `test_toml` 进行裂缝图片预测。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "3e3772fe",
   "metadata": {},
   "outputs": [],
   "source": [
    "import toml\n",
    "\n",
    "test_toml = r\"\"\"\n",
    "# 模型的整体配置文件。\n",
    "# 图片的文件路径。\n",
    "image_path = 'SurfaceCrack/test_data/'\n",
    "\n",
    "# 训练集中的数据个数，默认值为 -1 即使用全部数据。\n",
    "num_samples = 20\n",
    "\n",
    "# 训练好的模型参数文件的文件路径。\n",
    "model_path = 'qnnqd.pdparams'\n",
    "\n",
    "# 量子电路所包含的量子比特的数量。\n",
    "num_qubits = [4, 4]\n",
    "\n",
    "# 每一层量子电路中的电路深度。\n",
    "num_depths = [2, 2]\n",
    "\n",
    "# 量子电路中可观测量的设置。\n",
    "observables = [['Z0', 'Z1', 'Z2', 'Z3'], ['Z0', 'Z1', 'Z2', 'Z3']]\n",
    "\"\"\"\n",
    "\n",
    "config = toml.loads(test_toml)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "e5abd75c",
   "metadata": {},
   "source": [
    "在 `test_toml` 配置文件中：\n",
    "- `model_path`: 为训练好的模型，这里固定为 `qnnqd.pdparams`；\n",
    "- `num_qubits`、`num_depths`、`observables` 三个参数应与训练好的模型 ``qnnqd.pdparams`` 相匹配。`num_qubits = [4,4]` 表示量子部分一共两层电路；每层电路为 4 的量子比特；`num_depths = [2,2]` 表示每层参数化电路深度为 2；`observables` 表示每层测量算子的具体形式。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "c6c9f96b",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "d:\\anaconda3\\envs\\modellib\\lib\\site-packages\\paddle\\tensor\\creation.py:125: DeprecationWarning: `np.object` is a deprecated alias for the builtin `object`. To silence this warning, use `object` by itself. Doing this will not modify any behavior and is safe. \n",
      "Deprecated in NumPy 1.20; for more details and guidance: https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations\n",
      "  if data.dtype == np.object:\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "图片的预测结果分别为 0, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 0, 1, 1\n",
      "图片的实际标签分别为 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 1, 1, 0, 1, 1\n"
     ]
    }
   ],
   "source": [
    "from paddle_quantum.qml.qnnqd import inference\n",
    "\n",
    "prediction, prob, label = inference(**config)\n",
    "print(f\"图片的预测结果分别为 {str(prediction)[1:-1]}\")\n",
    "print(f\"图片的实际标签分别为 {str(label)[1:-1]}\")"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "5b1bc8f9",
   "metadata": {},
   "source": [
    "以上是模型对于测试集给出的预测结果。下面具体给出两张图片的预测细节"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "230c5283",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 导入所需要的包\n",
    "import os\n",
    "import warnings\n",
    "\n",
    "warnings.filterwarnings('ignore')\n",
    "os.environ['PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION'] = 'python'\n",
    "\n",
    "import numpy as np\n",
    "import paddle\n",
    "import matplotlib.pyplot as plt\n",
    "from paddle_quantum.qml.qnnqd import QNNQD, ImageDataset\n",
    "\n",
    "# 设置模型参数\n",
    "num_qubits = [4,4]\n",
    "num_depths = [2,2]\n",
    "observables = [['Z0','Z1','Z2','Z3'], ['Z0','Z1','Z2','Z3']]\n",
    "\n",
    "# 加载已训练的模型\n",
    "model = QNNQD(\n",
    "    num_qubits=num_qubits,\n",
    "    num_depths=num_depths,\n",
    "    observables=observables,\n",
    ")\n",
    "state_dict = paddle.load('./qnnqd.pdparams')\n",
    "model.set_state_dict(state_dict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "db11f93d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.image.AxesImage at 0x22af73712c8>"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAaAAAAGdCAYAAABU0qcqAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjUuMywgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/NK7nSAAAACXBIWXMAAA9hAAAPYQGoP6dpAAAiqklEQVR4nO3dfWyV5f3H8U9bek4faE8tpU9SWEGFKcI2Jh1B+eloeFhmRFnm0x9gDEZWzJA5DYuKbku6aeKMhuE/G8xM1JkIRM3IFKTEDVhACSHbGmi6AYOWyWwPnNIH2vv3B6HzCEivLz33dVrer+Qk9Jz72/u6r3Odfrh77vNtRhAEgQAACFmm7wEAAK5MBBAAwAsCCADgBQEEAPCCAAIAeEEAAQC8IIAAAF4QQAAAL0b4HsAX9fX16ejRoyooKFBGRobv4QAAHAVBoJMnT6qyslKZmRc/z0m7ADp69Kiqqqp8Dw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eFy7du3SjBkzBnNXAIAhzvkquFOnTungwYP9Xzc3N2vv3r0qLi7W2LFjtXz5cv385z/Xtddeq+rqaj311FOqrKzUggULBnPcAIAhzjmAdu/erdtuu63/6xUrVkiSFi1apHXr1unxxx9XIpHQQw89pLa2Nt18883avHmzcnJyBm/UAIAhLyOwdPZLoXg8rlgspk2bNjk1I7U087M2I7XsyxLALsd/juXpPH36tHONZGvUeObMGdO+XKV7M1JLc0zLfizNPqXzr2QdCEsD00Qi4VxjmQfL2KwsPx8sjYct82Ctc31dJBIJLViwQO3t7V/6vr73q+AAAFcmAggA4AUBBADwggACAHhBAAEAvCCAAABeEEAAAC8IIACAFwQQAMALAggA4AUBBADwggACAHhBAAEAvHBvwRqS7u5upw6xRUVFzvvIzc11rpFsHZ27urqca06dOuVc89lnn4VSI9k6Tlu6M588edK5xio7O9u5xtL92LIeLB20LWtIkq677jrnmoKCAueaeDzuXGMxcuRIU10sFnOusfwssnTLt3aWt6wj1/U60LFxBgQA8IIAAgB4QQABALwggAAAXhBAAAAvCCAAgBcEEADACwIIAOAFAQQA8IIAAgB4QQABALwggAAAXqRtM9LPPvtMnZ2dA97e0swvKyvLuUaSWltbnWuamppC2U8ikXCucZnnz2tra3OuCatZqrVRY2ZmOP8nszQwtdSMHj3auUaSjh496lwTVrNUy+s2IyPDuUaSSkpKnGuuv/5655rp06c717g0a77cOtc5D4JgQNtxBgQA8IIAAgB4QQABALwggAAAXhBAAAAvCCAAgBcEEADACwIIAOAFAQQA8IIAAgB4QQABALwggAAAXqRtM9I//elPys7OHvD25eXlzvuwNDCVpOPHjzvXWBqL9vb2OtdEo1HnGmujxsOHDzvXWJuEusrLyzPVWRo1WpqEWp7bgTZ4/DzLepBsDT8tz63lmCwNYy2NcyXbGm9ubnau2b9/v3ON9bm1vDZcf1Z2dXUNaDvOgAAAXhBAAAAvCCAAgBcEEADACwIIAOAFAQQA8IIAAgB4QQABALwggAAAXhBAAAAvCCAAgBcEEADAi7RtRvrf//7XqTGkp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UBBADwggACAHhBAAEAvCCAAABeEEAAAC8IIACAF2nXC+5cH6WOjg6nujB7oFn6uln6pll6SmVlZTnXZGdnO9dI7s+RZJtzSz8uy3NkZekzZnmeLOvB2gPN0pssrH5mw7EXXKr7Xobt3M+GSx1XRpBmR37kyBFVVVX5HgYA4DIdPnxYY8aMuejjaRdAfX19Onr0qAoKCs7730c8HldVVZUOHz6swsJCTyP0j3k4i3k4i3k4i3k4Kx3mIQgCnTx5UpWVlV969pl2v4LLzMz80sSUpMLCwit6gZ3DPJzFPJzFPJzFPJzlex5isdglt+EiBACAFwQQAMCLIRVA0WhUq1atUjQa9T0Ur5iHs5iHs5iHs5iHs4bSPKTdRQgAgCvDkDoDAgAMHwQQAMALAggA4AUBBADwYsgE0OrVq/WVr3xFOTk5qqmp0V//+lffQwrdM888o4yMjKTbpEmTfA8r5bZv367bb79dlZWVysjI0MaNG5MeD4JATz/9tCoqKpSbm6va2lodOHDAz2BT6FLzsHjx4vPWx7x58/wMNkXq6+t10003qaCgQKWlpVqwYIEaGxuTtuns7FRdXZ1GjRqlkSNHauHChWptbfU04tQYyDzceuut562Hhx9+2NOIL2xIBNCbb76pFStWaNWqVfr44481depUzZ07V8ePH/c9tNDdcMMNOnbsWP/to48+8j2klEskEpo6dapWr159wcefe+45vfTSS3rllVe0a9cu5efna+7cuers7Ax5pKl1qXmQpHnz5iWtj9dffz3EEaZeQ0OD6urqtHPnTr3//vvq6enRnDlzlEgk+rd59NFH9c477+itt95SQ0ODjh49qrvuusvjqAffQOZBkpYsWZK0Hp577jlPI76IYAiYPn16UFdX1/91b29vUFlZGdTX13scVfhWrVoVTJ061fcwvJIUbNiwof/rvr6+oLy8PHj++ef772trawui0Wjw+uuvexhhOL44D0EQBIsWLQruuOMOL+Px5fjx44GkoKGhIQiCs899dnZ28NZbb/Vv8/e//z2QFOzYscPXMFPui/MQBEHwf//3f8EPf/hDf4MagLQ/A+ru7taePXtUW1vbf19mZqZqa2u1Y8cOjyPz48CBA6qsrN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GuzevTtobm4ONm3aFIwfPz6YNWuW55EnGxIBFARB8PLLLwdjx44NIpFIMH369GDnzp2+hxS6u+++O6ioqAgikUhw9dVXB3fffXdw8OBB38NKuQ8//DCQdN5t0aJFQRCcvRT7qaeeCsrKyoJoNBrMnj07aGxs9DvoFPiyeejo6AjmzJkTjB49OsjOzg7GjRsXLFmyZNj9J+1Cxy8pWLt2bf82p0+fDn7wgx8EV111VZCXlxfceeedwbFjx/wNOgUuNQ+HDh0KZs2aFRQXFwfRaDS45pprgh//+MdBe3u734F/AX+OAQDgRdq/BwQAGJ4IIACAFwQQAMALAggA4AUBBADwggACAHhBAAEAvCCAAABeEEAAAC8IIACAFwQQAMALAggA4MX/Axp8bMAGbhcOAAAAAElFTkSuQmCC",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "crack_img = plt.imread('positive_crack.jpg') # Positive, label=[1.0,0.0]\n",
    "plt.imshow(crack_img, cmap='gray', vmin=0, vmax=255)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "563d6922",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.image.AxesImage at 0x22afea18408>"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAaAAAAGdCAYAAABU0qcqAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjUuMywgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/NK7nSAAAACXBIWXMAAA9hAAAPYQGoP6dpAAAgZklEQVR4nO3df2xV9f3H8Vcp7aWUtqzW/hqFFfzBJtJlTLoGZTgaKEuMKH/46w8wBiMrZsichkVFtyXdMHFGwxf/2WAmos5EIJqNRYstcwMWUELMtoaSbkCgZZLQ0gLtbXu+fxA7r/zqeXPved+W5yO5ib09n57P+dxz+/Jy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JLU0dGRcH9HR8fQ974qFospPz8/4QYAGP2SGkCVlZUqLS1VY2Pj0H1dXV3as2ePampqkrkrAMAIF/oquO7ubrW2tg593dbWpv3796uwsFCTJ0/WqlWr9Mtf/lI33nijKisr9eyzz6q8vFyLFy9O5rwBACNc6ADau3ev7rzzzqGvV69eLUlaunSpNm3apKeeeko9PT169NFHderUKd1+++3avn27xo0bl7xZAwBGvIwgqra9Yerq6lJBQYGampo0YcKEYY+L8jDGjg3f4RqLxUKPsZSRWkoNrWvX398fyb6ifGyjKoWMah2sZaQWUR2TZYyl0DZK1vLcdNXd3a3q6mp1dnZe9n390XXUAIARgwACALgggAAALgggAIALAggA4IIAAgC4IIAAAC4IIACACwIIAOCCAAIAuCCAAAAuCCAAgAsCCADgInytc0QyMzNDNfmmext2VK3EUTYFW9q6BwYGIhkTpaganS3rEFW7d5Qsx2R5zsJuuO3evAICALgggAAALgggAIALAggA4IIAAgC4IIAAAC4IIACACwIIAOCCAAIAuCCAAAAuCCAAgAsCCADgYtQ09FkKCq0FocMt2rta1pLQqPaTzkWX1nJayzFZxljWPKoCUyvL8yKdzyEpuvlFWaacTngFBABwQQABAFwQQAAAFwQQAMAFAQQAcEEAAQBcEEAAABcEEADABQEEAHBBAAEAXBBAAAAXBBAAwEXalpH29/erv79/2NtbikWtRYOW0kVL2aBlTFRlmpJCPT5Rj7EaOzb8U8KyfpZzyFJgGlVxrlVU57i17DOqktAoS2OjMNznbHqfnQCAUYsAAgC4IIAAAC4IIACACwIIAOCCAAIAuCCAAAAuCCAAgAsCCADgggACALgggAAALgggAICLtC0jHRgYCFVCORpLF6NiKXKVpHg8HnqMpVjUUsJpLViNqhzTIsqi2ahEVdJrOYeilM6/HyyGezzp/VsbADBqEUAAABehA2jnzp266667VF5eroyMDG3dujXh+8uWLVNGRkbCra6uLlnzBQCMEqEDqKenR1VVVVq/fv0lt6mrq9Px48eHbm+++eZVTRIAMPqEvghh0aJFWrRo0WW3icViKi0tNU8KADD6peQ9oKamJhU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pdbiY6upqSUqr8yHtAyg7O1uzZs1SY2Pj0H2Dg4NqbGxUTU2N48z8dXd369ChQyorK/OeipvKykqVlpYmnB9dXV3as2fPNX9+HD16VCdPnhxV50cQBFq5cqW2bNmiHTt2qLKyMuH7s2bNUlZWVsL50NLSosOHD4+q8+FK63Ax+/fvl6T0Oh+8r4IYjrfeeiuIxWLBpk2bgn/84x/Bo48+GkycODFob2/3nlqkfvKTnwRNTU1BW1tb8Ne//jWora0NioqKghMnTnhPLaVOnz4dfPrpp8Gnn34aSApeeuml4NNPPw3+85//BEEQBL/61a+CiRMnBtu2bQsOHDgQ3H333UFlZWVw9uxZ55kn1+XW4fTp08GTTz4Z7Nq1K2hraws+/PDD4Dvf+U5w4403BufOnfOeetKsWLEiKCgoCJqamoLjx48P3c6cOTO0zWOPPRZMnjw52LFjR7B3796gpqYmqKmpcZx18l1pHVpbW4Of//znwd69e4O2trZg27ZtwdSpU4O5c+c6zzzRiAigIAiCV199NZg8eXKQnZ0dzJ49O9i9e7f3lCJ33333BWVlZUF2dnbw9a9/PbjvvvuC1tZW72ml3EcffRRIuuC2dOnSIAjOX4r97LPPBiUlJUEsFgvmz58ftLS0+E46BS63DmfOnAkWLFgQXH/99UFWVlYwZcqUYPny5aPuf9IudvySgo0bNw5tc/bs2eBHP/pR8LWvfS0YP358cM899wTHjx/3m3QKXGkdDh8+HMydOzcoLCwMYrFYcMMNNwQ//elPg87OTt+JfwV/jgEA4CLt3wMCAIxOBBAAwAUBBABwQQABAFwQQAAAFwQQAMAFAQQAcEEAAQBcEEAAABcEEADABQEEAHBBAAEAXPw/rb5EgupsquoAAAAASUVORK5CYII=",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "non_crack_img = plt.imread('negative_non_crack.jpg') # Negative, label=[0,1]\n",
    "plt.imshow(non_crack_img, cmap='gray', vmin=0, vmax=255)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "85369b8e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "对于裂缝图片，模型有 99.99% 的置信度检测出其质量受损。\n",
      "对于无裂缝的图片，模型有 77.33% 的置信度检测出其质量无损。\n"
     ]
    }
   ],
   "source": [
    "# 将图片编码为量子态\n",
    "preprocess = ImageDataset(file_path='SurfaceCrack/training_data/', num_samples=500)\n",
    "\n",
    "crack_img_data = np.reshape(crack_img, 784)\n",
    "non_crack_img_data = np.reshape(non_crack_img, 784)\n",
    "test_data = np.array([crack_img_data, non_crack_img_data])\n",
    "\n",
    "test_data = preprocess.centering.transform(test_data)\n",
    "test_data = preprocess.pca.transform(test_data)\n",
    "test_data = preprocess.scaler.transform(test_data)\n",
    "\n",
    "test_data = paddle.to_tensor(test_data, dtype='float64')\n",
    "\n",
    "# 使用模型进行预测并得到对应的概率值\n",
    "prob = model(test_data)\n",
    "print(f\"对于裂缝图片，模型有 {prob[0][0].item():3.2%} 的置信度检测出其质量受损。\")\n",
    "print(f\"对于无裂缝的图片，模型有 {prob[1][1].item():3.2%} 的置信度检测出其质量无损。\")"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "8f6f3b91",
   "metadata": {},
   "source": [
    "## 注意事项\n",
    "\n",
    "我们通常考虑调整 `num_qubits`、`num_depths`、`observables` 三个主要超参数，对模型的影响较大。"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "813ba2ae",
   "metadata": {},
   "source": [
    "## 引用\n",
    "\n",
    "[1] Özgenel, Çağlar Fırat (2019), “Concrete Crack Images for Classification”, Mendeley Data, V2, doi: 10.17632/5y9wdsg2zt.2 "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "modellib",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.15"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "vscode": {
   "interpreter": {
    "hash": "dfa0523b1e359b8fd3ea126fa0459d0c86d49956d91b464930b80cba21582eac"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
